

#import "PieChartViewController.h"
#import "PCPieChart.h"
#import "BIDAppDelegate.h"

@implementation PieChartViewController

- (id)init
{
	self = [super init];
	if (self)
	{
		[self.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
		[self setTitle:@"Pie Chart"];
		
		
		int height = [self.view bounds].size.width/3*2.; // 220;
		int width = [self.view bounds].size.width; //320;
		PCPieChart *pieChart = [[PCPieChart alloc] initWithFrame:CGRectMake(([self.view bounds].size.width-width)/2,([self.view bounds].size.height-height)/2,width,height)];
		[pieChart setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
		[pieChart setDiameter:width*3/5];
		[pieChart setSameColorLabel:YES];
		
		[self.view addSubview:pieChart];
		
		if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad)
		{
			pieChart.titleFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
			pieChart.percentageFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:50];
		}
        BIDAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        //calculate dev of the month by number of line code per month of a dev
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MonthlyData"];
        NSError *error;
        NSArray *objects = [context executeFetchRequest:request error:&error];
        if (objects == nil){
            NSLog(@"Monthly Data in local storage is empty !");
            return nil;
        }
        //NSLog(@"%d", [objects count]);
        NSMutableDictionary *sampleInfo = [[NSMutableDictionary alloc] initWithCapacity:5];
        for (NSManagedObject *object in objects){
            NSString *name = [[NSString alloc] initWithString:[object valueForKey:@"name"]];
            NSInteger lineCode = 0;
            for (int i = 1; i <= 31; i++){
                NSString *day;
                if (i < 10){
                    day = [[NSString alloc] initWithFormat:@"d0%d", i];
                }else{
                    day = [[NSString alloc] initWithFormat:@"d%d", i];
                }
                lineCode += [[object valueForKey:day] intValue];
            }
            [sampleInfo setObject:[NSNumber numberWithFloat:lineCode] forKey:name];
        }
		NSMutableArray *components = [NSMutableArray array];
        int i =0;
		for (NSString *key in [sampleInfo allKeys])
		{
            //NSLog(@"%@",key);
            //NSLog(@"%d", [[sampleInfo objectForKey:key] intValue]);
			PCPieComponent *component = [PCPieComponent pieComponentWithTitle:key value:[[sampleInfo objectForKey:key] intValue]];
			[components addObject:component];
			
			if (i==0)
			{
				[component setColour:PCColorYellow];
			}
			else if (i==1)
			{
				[component setColour:PCColorGreen];
			}
			else if (i==2)
			{
				[component setColour:PCColorOrange];
			}
			else if (i==3)
			{
				[component setColour:PCColorRed];
			}
			else if (i==4)
			{
				[component setColour:PCColorBlue];
			}
            i++;
		}
		[pieChart setComponents:components];
	}
	return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



@end
